﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin.Win32;

namespace MyQQ
{
    partial class MainForm :CCWin.CCSkinMain
    {
        public MainForm(string name)
        {
            InitializeComponent();
            this.name = name;
            this.lableName.Text = name;
        }

        private Dictionary<string, ChatForm> chatRooms = new Dictionary<string, ChatForm>(64);

        private string name;

        private List<string> onlineFriend;
        private List<string> offlineFriend;

        public void GetInfo(Message.Message msg)
        {
            if (msg.Type == Message.MessageType.Login)
            {
                onlineFriend = msg.ActiveName;
                offlineFriend = msg.InActiveName;
                refresh();
            }
            if (msg.Type == Message.MessageType.IsActive)
            {
                msg.IsLive = true;
                onlineFriend = msg.ActiveName;
                offlineFriend = msg.InActiveName;
                refresh();
                Net.NetSender.Sender.SendMessage(msg);
            }
            if (msg.Type == Message.MessageType.Message)
            {
                string toWho = msg.FromWho.Name;
                if (chatRooms[toWho] == null)
                {
                    ChatForm cf = new ChatForm(name, toWho);
                    chatRooms[toWho] = cf;
                    cf.GetInfo(msg);
                   // Net.NetReciver.Listener.MessageArrived += cf.GetInfo;
                }
                chatRooms[toWho].Visible = true;
                chatRooms[toWho].Show();
            }
        }
        private void refresh()
        {
            CCWin.SkinControl.ChatListItemCollection items = this.listFriend.Items;
            items[0].SubItems.Clear();
            items[1].SubItems.Clear();
            foreach (var r in this.onlineFriend)
            {
                CCWin.SkinControl.ChatListSubItem item = new CCWin.SkinControl.ChatListSubItem();
                item.NicName = name;
                item.DisplayName = name;
                item.HeadImage = MyQQ.Properties.Resources._1_100;
                
                items[0].SubItems.Add(item);
            }

            foreach (var r in this.offlineFriend)
            {
                CCWin.SkinControl.ChatListSubItem item = new CCWin.SkinControl.ChatListSubItem();
                item.NicName = name;
                item.DisplayName = name;
                item.HeadImage = MyQQ.Properties.Resources._1_100;
                item.Status = CCWin.SkinControl.ChatListSubItem.UserStatus.OffLine;
                items[1].SubItems.Add(item);
            }
        }

        private void listFriend_DoubleClickSubItem(object sender, CCWin.SkinControl.ChatListEventArgs e)
        {
            string toWho = e.SelectSubItem.NicName;
            if (e.SelectSubItem.Status == CCWin.SkinControl.ChatListSubItem.UserStatus.OffLine)
                return;
            if (!chatRooms.Keys.ToList().Contains(toWho))
            {
                ChatForm cf = new ChatForm(name, toWho);
                chatRooms[toWho] = cf;
            }
            chatRooms[toWho].Visible = true;
            chatRooms[toWho].Show();
        }

        


    }
}
